﻿using System.Web.Mvc;
using Model.Request;
using JF.WebSite.ViewModels;
using System.Web;
using System;
using JF.WebSite.App_Start;
using Common; 
namespace JF.WebSite.Controllers
{

    public class HomeController : Controller
    {
        private log4net.ILog log = log4net.LogManager.GetLogger("HomeController");
        [CheckLogin]
        public ActionResult Index()
        { 
            return View();
        }

        public ActionResult Login()
        { 
            HttpCookie cookie = Request.Cookies["UserCook"];
            string name = string.Empty;
            string pwd = string.Empty;
            string cb = string.Empty;
            if (cookie != null && cookie.Values["UserPwd"] != null)
            {
                name = Utils.DecryptDES(cookie.Values["Alias"], "zhongw66");
                pwd = Utils.DecryptDES(cookie.Values["UserPwd"], "zhongw66");
                cb = "checked";
            }
            ViewBag.name = name;
            ViewBag.pwd = pwd;
            ViewBag.cb = cb;
            return View();
        }
        [HttpPost]
        public ActionResult UserLogin(LoginRequest request)
        {
            ResultModel<string> res = new ResultModel<string>();
            BLL.UsersBLL bll = new BLL.UsersBLL();
            try
            {
                var userinfo = bll.Login(request);
                if (userinfo != null)
                {
                    if (userinfo.Result == 1)
                    {
                        res.Code = 1;
                        res.Msg = userinfo.Message;
                        res.Data = string.Empty;
                        HttpCookie cookie = new HttpCookie("UserCook");//初使化Cookie
                        DateTime dt = DateTime.Now;
                        //TimeSpan ts = new TimeSpan(0, 0, 30, 0, 0);//过期时间为30分钟
                        //cookie.Expires = dt.Add(ts);//设置过期时间  
                        cookie.Values.Add("ShowCostPrice", userinfo.ShowCostPrice);
                        cookie.Values.Add("DeviceID", userinfo.DeviceID);
                        cookie.Values.Add("Alias", Utils.EncryptDES(userinfo.Alias, "zhongw66"));
                        cookie.Values.Add("UserCode", userinfo.UserCode);
                        cookie.Values.Add("UserName", Utils.EncryptDES(userinfo.UserName, "zhongw66"));
                        cookie.Values.Add("CoID", userinfo.CoID.ToString());
                        cookie.Values.Add("CompanyName", Utils.EncryptDES(userinfo.CompanyName, "zhongw66"));
                        cookie.Values.Add("Mobile", userinfo.Mobile);
                        cookie.Values.Add("wxCode", userinfo.wxCode);
                        if (request.cb == "1")
                        {
                            cookie.Values.Add("UserPwd", Utils.EncryptDES(request.pwd, "zhongw66"));
                        }
                        Response.AppendCookie(cookie);
                        return new JsonResult() { Data = res };
                    }
                    else
                    {
                        res.Code = 0;
                        res.Msg = userinfo.Message;
                        res.Data = string.Empty;
                        return new JsonResult() { Data = res };
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("UserLogin" + ex.ToString());
            }
            res.Code = 0;
            res.Msg = "系统异常，请联系管理员！";
            res.Data = string.Empty;
            return new JsonResult() { Data = res };
        }

        public ActionResult Error()
        {
            return View();
        }
    }
}